package com.zzw.system.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzw.model.system.SysUser;
import com.zzw.vo.system.RouterVo;
import com.zzw.vo.system.SysUserQueryVo;
import com.zzw.system.mapper.SysUserMapper;
import com.zzw.system.service.SysMenuService;
import com.zzw.system.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author zzw
 * @since 2023-04-20
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    SysMenuService sysMenuService;

    @Override
    public IPage<SysUser> selectPage(Page<SysUser> pageParam, SysUserQueryVo userQueryVo) {
        return baseMapper.selectPage(pageParam,userQueryVo);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        SysUser sysUser = baseMapper.selectById(id);
        sysUser.setStatus(status);
        baseMapper.updateById(sysUser);
    }

    @Override
    public SysUser getByUserName(String username) {
        return baseMapper.selectOne(new QueryWrapper<SysUser>().eq("username",username));
    }


    //        根据用户名获取用户（基本信息，菜单权限，按钮权限等）
    @Override
    public Map<String, Object> getUserInfo(String username) {

//        根据username获取用户
        SysUser sysUser = this.getByUserName(username);
//        根据用户id获取菜单权限数据
        List<RouterVo> routerVolist=sysMenuService.getUserMenuList(sysUser.getId());
//        根据用户id获取按钮权限数据
        List<String> premsList=sysMenuService.getUserButtonList(sysUser.getId());

        Map<String,Object> result=new HashMap<>();
        result.put("username",username);
        result.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        //当前权限控制使用不到，我们暂时忽略
        result.put("roles",  new HashSet<>());

//        菜单权限数据
        result.put("routers",routerVolist);
//        按钮权限数据
        result.put("buttons",premsList);

        return result;
    }
}
